package com.ecarx.vhaltests.hy11.testcases.unit;

import android.car.VehicleAreaType;
import android.car.VehiclePropertyIds;
import android.car.VehicleUnit;
import android.util.Log;

import com.ecarx.vhaltests.IpcpClient;
import com.ecarx.vhaltests.VehicleConstants;
import com.ecarx.vhaltests.VhalUnitTestBase;

import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;


@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class DistanceDisplayUnitTest extends VhalUnitTestBase {

    private static final String TAG = "DistanceDisplayUnitTest";

    private static final int mPropertyId = VehiclePropertyIds.DISTANCE_DISPLAY_UNITS;

    private static final int mUNITS_AREA = VehicleAreaType.VEHICLE_AREA_TYPE_GLOBAL;

    private static final int SERVICE_ID_VGM = 49;
    private static final int OP_ID_VGM = 999;


    @BeforeClass
    public static void testInit() {
        setConfig(OP_ID_VGM);
        delay();
    }

    @Test
    public void testDistanceDisplayUnitTestAvail() {
        setAvail(true);
        delay();
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(mPropertyId, mUNITS_AREA);
        Log.d(TAG, "isAvail：" + isAvail);
        Assert.assertEquals(true ,isAvail);

        //test set
        mCarPropertyMgr.setIntProperty(mPropertyId, mUNITS_AREA, VehicleUnit.KILOMETER);
        delay();
        int value = mCarPropertyMgr.getIntProperty(mPropertyId, mUNITS_AREA);
        Assert.assertEquals(VehicleUnit.KILOMETER, value);
        delay();
        mCarPropertyMgr.setIntProperty(mPropertyId, mUNITS_AREA, VehicleUnit.MILE);
        delay();
        value = mCarPropertyMgr.getIntProperty(mPropertyId, mUNITS_AREA);
        Assert.assertEquals(VehicleUnit.MILE, value);

    }

    @Test
    public void testDistanceDisplayUnitTestUnAvail() {
        setAvail(false);
        delay();
        boolean isAvail = mCarPropertyMgr.isPropertyAvailable(mPropertyId, mUNITS_AREA);
        Log.d(TAG, "isAvail：" + isAvail);
        Assert.assertEquals(false ,isAvail);
    }

    public void setAvail(boolean avail) {
        Log.d(TAG, "setAvail avail：" + avail);
        IpcpClient ipcpClient = new IpcpClient();
        String[] vgms = {"vehModMngtGlbSafe1UsgModSts"};
        boolean setVGMSignalStatus;

        if (!avail) {
            int[] vgmValuesNotAvail = {VehicleConstants.USG_MODE_ABANDON};
            setVGMSignalStatus = ipcpClient.setVGMSignal(SERVICE_ID_VGM, OP_ID_VGM, vgms, vgmValuesNotAvail);
        } else {
            int[] vgmValuesAvail = {VehicleConstants.USG_MODE_DRIVING};
            setVGMSignalStatus = ipcpClient.setVGMSignal(SERVICE_ID_VGM, OP_ID_VGM, vgms, vgmValuesAvail);
        }
        Assert.assertTrue(setVGMSignalStatus);
    }


    private  static void setConfig(int OperId) {
        IpcpClient ipcpClient = new IpcpClient();
        int climateOperId[] = {OperId};
        ipcpClient.setConfig(SERVICE_ID_VGM, climateOperId);
    }

}
